/*
 * ses_alarmclock.h
 *
 *  Created on: 2013-6-29
 *      Author: yangyang
 */

#ifndef SES_ALARMCLOCK_H_
#define SES_ALARMCLOCK_H_

/*INCLUDES *******************************************************************/
#include "ses_common.h"

/*TYPE DEFINITION**************************************************************/

typedef struct Event Event;
typedef struct Fsm Fsm;

/* a state is represented by a function pointer */
typedef void (*State)(Fsm *, const Event *);

/* base type for events*/
struct Event {
	uint8_t signal;
};

/* base type for state machine*/
struct Fsm {
	State state; 		//current state
	uint8_t alarmSet; 	// is alarm enabled ?
	uint32_t timeSet; 	// temporary buffer for time
};

/*event signal used by clock*****************************************************/

enum {
	JOYSTICK_BUTTON = 1, ROTARY_BUTTON, TIME_OUT, WAIT_30S
};

/*FUNCITION DECLEARION************************************************************/

/*
 * Finite State Machines (FSM)
 * dispatch and Initialization
 */
void fsmdispatch(Fsm* fsm, const Event* event);
void fsminit(Fsm* fsm, State init);

/*
 * Alarm Clock initialization
 * LCD / Scheduler / Button / Timer2 / Buzzer / Leds
 * Enable interrupts
 * Set RTC
 */
void alarmclock_init(Fsm *fsm, const Event *event);

/*
 * Set Alarm Clock to Normal by setting the state
 * check if signal is @ROTARY_BUTTON, then set fsm state to @setalarm
 */
void alarmclock_normal(Fsm *fsm, const Event *e);

/*
 * Clock setting up
 * by prompting the User to enter the Time
 */
void alarmclock_setrtc(Fsm *fsm, const Event *e);

/*
 * Set Alarm by @settime function by sending as arguments
 * Finite State machine @fsm / and the event @e
 */
void alarmclock_setalarm(Fsm *fsm, const Event *e);

/*
 * Turn the Alarm Clock if
 * @TIME_OUT: Buzzer-On / set FSM-state = @alarm clock buzz
 * @ROTARY_BUTTON : Reset Alarm Time / set FSM-state = @alarm clock normal
 */
void alarmclock_alarmon(Fsm *fsm, const Event *e);

/*
 * Alarm Clock Buzz is used to check if Event Signal is @WAIT_30s
 *  then it:
 *  deactivates Buzzer / remove Red Led toggling / Clock mode to Normal
 */
void alarmclock_buzz(Fsm *fsm, const Event *e);
void changemode(void);

/*
 * Function used to Set Hour/Min
 * by incrementing Hour or Min depending on the Case we are at
 * @return the set time (Hour/Mi)
 */
uint32_t set_hourmin(uint32_t);
void setflag();

/*
 * Function responsible for printing RTC on LCD
 */
void displayRTC();

/*
 * Function @settime used to set time using @tempsettime variable
 * after converting it from @timeset structure
 * It checks which Time to set by checking Event Signal
 * if Joystick -> increments Hour/Min
 * if Rotary -> flip between setting Time clock or Alarm clock
 */
void settime(Fsm *fsm, const Event *e);

#endif /* SES_ALARMCLOCK_H_ */
